/*
 * Revision.java
 *
 * Created on 15 de abril de 2007, 18:01
 */

package servidor;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import java.util.*;

import java.text.DateFormat;
import org.w3c.dom.NodeList;

/**
 *
 * @author Pellizard
 */
public class Revision {
    
    private Version version;
    private Date date;
    private Author author;
    private Vector authors;
    private String description;
    
    /** Creates a new instance of Revision */
    public Revision( Version version, Date fecha, Vector autores, String descripcion ) {
        
        this.version = version;
        this.date = fecha;
        this.authors=autores;
        this.description = descripcion;
    }

    public Version getVersion() {
        return version;
    }

    public void setVersion(Version version) {
        this.version = version;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date fecha) {
        this.date = fecha;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author autor) {
        this.author = autor;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String descripcion) {
        this.description = descripcion;
    }
    
    public Node toXml(){
      try
      {
          DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
          DocumentBuilder parser = fact.newDocumentBuilder();
          Document doc = parser.newDocument();

          return toXml( doc );
      }
      catch( Exception ex )
      {
         return null;
      }
  }
    
    public Node toXml( Document doc ){
        
      try {
      Node root = doc.createElement("revision");
      //doc.appendChild(root);

      root.appendChild(version.toXml(doc));
      
      Node fecha = doc.createElement("date");
      fecha.appendChild(doc.createTextNode(DateFormat.getDateInstance().format(this.date)));
      root.appendChild(fecha);
      
      Node autores = doc.createElement("authorsRev");
      for(int i=0;i<authors.size();i++){
          author = (Author) authors.get(i);
          autores.appendChild(author.toXml(doc));
      }
      
      root.appendChild(autores);
      
      Node descripcion = doc.createElement("description");
      descripcion.appendChild(doc.createTextNode(this.description));
      root.appendChild(descripcion);
      
      return root;

    } catch (Exception ex) {
      System.err.println("+============================+");
      System.err.println("|        XML Error           |");
      System.err.println("+============================+");
      System.err.println(ex.getClass());
      System.err.println(ex.getMessage());
      System.err.println("+============================+");
      ex.printStackTrace();
      return null;
    }
  }  
    
    public static Revision fromXml( String str )
  {
        ByteArrayInputStream is;
        try{
             is = new ByteArrayInputStream( str.getBytes("UTF8") );
        }catch(Exception e){
            return null;
        }
        
        try
        {
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = fact.newDocumentBuilder();
            Document doc = parser.parse( is );
            
            return fromXml( doc );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }
    
    public static Revision fromXml( Document doc )
  {
        String desc = "";
        Version v = new Version();
        Vector a = new Vector();
        Date d = new Date();
        
        try
        {
            NodeList nodes = doc.getElementsByTagName( "desc" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                desc = node.getFirstChild().getNodeValue();
            }
            
            nodes = doc.getElementsByTagName( "version" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                v = Version.fromXml( Util.node2String(node) );
            }
            
            nodes = doc.getElementsByTagName( "author" );
            
            for( int i = 0 ; i < nodes.getLength() ; i++ )
            {
                Node node = nodes.item( i );
                a.add( Author.fromXml( Util.node2String( node ) ) );
            }
            
            nodes = doc.getElementsByTagName( "date" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                d = new Date();
                //d = DateFormat.getInstance().parse( node.getNodeValue() );
            }
            
            return new Revision( v, d, a,desc );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }

}
